import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Set;


public class aNode implements Iterable<aNode> {
	
	private String word;
	private HashSet<aNode> edges;
	private char[] chars;
	
	public aNode(String str) {
		this.word = str;
		this.edges = new HashSet<aNode>();
			
		setChars();
	}
	
	/**
	 * Add an edge
	 * @param node
	 */
	public void addEdge(aNode node) {
		edges.add(node);
	}

	/**
	 * Get the word of this node
	 * @return
	 */
	public String getWord() {
		return word;
	}

	/**
	 * Find all the edges this node has in a given graph
	 * @param graph
	 */
	public void findEdges(Set<aNode> graph) {
		for(aNode node : graph) {
			
			if (node.getWord().equals("other")) {
				System.out.println("!");
			}
			
			Boolean link = true;
			
			//Check for same
			if (node == this) {
				link = false;
			}
			
			//Check all characters
			char[] tempWord = node.getWord().toCharArray();
			for(char c : chars) {
				int index = Arrays.binarySearch(tempWord,c);
				if (index<0) {
					link = false;
				}else{
					tempWord[index] = '0';
				}
			}
			
			if (link) {
				edges.add(node);
			}
		}
	}
	
	/**
	 * Set the key characters of this word
	 */
	private void setChars() {
		chars = new char[4];
		word.getChars(word.length()-4,word.length(),chars,0);
	}
	
	@Override
	public String toString() {
		
		String str = word + ":";
		
		for(aNode node : edges)
			str+= node.getWord() + "-";
		
		return str;
	}

	@Override
	public Iterator<aNode> iterator() {
		return edges.iterator();
	}
}
